<?php
namespace Admin\Controller;

use Think\Controller;
use Common\Util\UploadHandler;

class PublicController extends Controller {
	
	function _initialize() {}
    
    public function verify() {
    	ob_clean();
    	$Verify = new \Think\Verify(array(
    		'length' => 4, 
    		'codeSet' => '0123456789',
    		'imageH' => 35,
    		'imageW' => 115,
    		'fontSize' => 16
    	));
    	$Verify->entry();
    }

    /**
     * @todo 管理员登录
     */
    public function login() {
    	if (!IS_POST) {
    		if (!isset($_SESSION ['admin_user'])) {
    			$redirect = I ('get.redirect');
    			if ($redirect) {
    				$this->assign('redirect', $redirect);
    			}
    			
    			$this->display();
    		} else {
    			return redirect(U('/Admin/Index/index'));
    		}
    	} else {
    		$Verify = new \Think\Verify();
    		$code = I ('post.code');
    		if (!$Verify->check($code)) {
    			$this->error('验证码有误！');
    		}
    		
    		$username = I ('post.account');
    		$password = I ('post.password');
    		
    		$adminUser = D('AdminUser');
    		$adminUserInfo = $adminUser->login($username, $password);
    		if (!$adminUserInfo) {
    			$this->error('账号密码错误！');
    		} else {
    			$_SESSION ['ADMIN_ID'] = $adminUserInfo ['id'];
    			$_SESSION ['admin_user'] = serialize($adminUserInfo);
    			
    			$operation = array('remark' => 'login');
    			\Think\Hook::listen('operation', $operation);
    			
    			$url = U('/Admin/Index/index');
    			$redirect = I ('post.redirect');
    			
    			if ($redirect) {
    				$url = $redirect;
    			}
    			redirect($url);
    		}
    	}
    }
    
    public function logout() {
    	unset($_SESSION ['admin_user']);
    	
    	redirect(U('/Admin/Public/login'));
    }
}